package com.soft.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

// Referenced classes of package com.social.util:
//			DbHelper, MovieTempVo

public class XmlHelper extends org.xml.sax.helpers.DefaultHandler
{

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(XmlHelper.class);
	private java.util.Stack tags;
	private static int i = 1;
	private DbHelper dbHelper;
	private MovieTempVo movie;
	private java.lang.StringBuilder sb;

	public XmlHelper()
	{
		tags = new Stack();
		dbHelper = new DbHelper();
		movie = null;
		sb = new StringBuilder();
	}

	public void startDocument()
		throws org.xml.sax.SAXException
	{
		log.info("开始解析");
	}

	public void characters(char ch[], int start, int length)
		throws org.xml.sax.SAXException
	{
		java.lang.String tag = (java.lang.String)tags.peek();
		java.lang.String value = new String(ch, start, length);
		if (tag.equals("name"))
			movie.setName(value);
		else
		if (tag.equals("en_name"))
			movie.setEnName(value);
		else
		if (tag.equals("i"))
		{
			java.util.List names = movie.getOtherNames();
			names.add(value);
		} else
		if (tag.equals("source"))
			movie.setSource(value);
		else
		if (tag.equals("release-date"))
			movie.setReleaseDate(value);
		else
		if (tag.equals("director"))
		{
			java.util.List ds = movie.getDirectors();
			ds.add(value);
		} else
		if (tag.equals("actor"))
		{
			java.util.List list = movie.getActors();
			list.add(value);
		} else
		if (tag.equals("type"))
		{
			java.util.List list = movie.getTypes();
			list.add(value);
		} else
		if (tag.equals("poster"))
		{
			java.util.List list = movie.getPosters();
			list.add(value);
		} else
		if (tag.equals("p"))
			sb.append("<p>").append(value).append("</p>");
	}

	public void endDocument()
		throws org.xml.sax.SAXException
	{
		log.info("成功结束任务!");
	}

	public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String name)
		throws org.xml.sax.SAXException
	{
		java.lang.String popName = (java.lang.String)tags.pop();
		if (name.equals("summary"))
		{
			movie.setSummary(sb.toString());
			sb = new StringBuilder();
		}
//		if (name.equals("movie"))
//			dbHelper.save(movie);
	}

	public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes attributes)
		throws org.xml.sax.SAXException
	{
		tags.push(name);
		if (name.equals("movie"))
		{
			movie = new MovieTempVo();
			movie.setId(java.lang.String.valueOf(i));
			i++;
			movie.setTypes(new ArrayList());
			movie.setDirectors(new ArrayList());
			movie.setActors(new ArrayList());
			movie.setPosters(new ArrayList());
			movie.setOtherNames(new ArrayList());
		}
	}

}